package com.frejoys.store.controller;

import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.app.AddStoreEnterDto;
import com.frejoys.common.dto.app.UpStoreEnterDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.app.StoreEnterVo;
import com.frejoys.service.StoreEnterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "商家入驻", description = "商家入驻")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixStore)
@Authorization(Authority.Private)
public class MallStoreEnterController {
    private final RedissonClient redissonClient;

    private final StoreEnterService storeEnterService;

    @PostMapping("/storeEnter/insertStoreEnter")
    @Operation(summary = "商家入驻申请")
    public Result insertStoreEnter(@RequestBody @Validated AddStoreEnterDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.storeEnterLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeEnterService.insertStoreEnter(RequestUtil.userId(),dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


    @PostMapping("/storeEnter/upStoreEnter")
    @Operation(summary = "修改商家入驻申请")
    public Result upStoreEnter(@RequestBody @Validated UpStoreEnterDto dto){
        RLock lock = redissonClient.getLock(CacheKeyUtil.upStoreEnterLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {
                storeEnterService.upStoreEnter(dto);
            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


    @PostMapping("/storeEnter/queryStoreEnter")
    @Operation(summary = "查询用户申请入驻记录")
    public Result<StoreEnterVo> queryStoreEnter(){
        return Result.success(storeEnterService.queryStoreEnter(RequestUtil.userId()));
    }
}
